﻿using Prism.Services.Dialogs; 
using WPF.Core;
using WPF.Core.Dtos;
using WPF.Core.Services;

namespace WPF.NoteBook.ViewModels
{
    internal class CreateNoteBookViewModel : HostDialogViewModel
    {
        public CreateNoteBookViewModel(INoteBookService appService)
        {
            this.appService = appService;
        }

        private NoteBookModel model;
        private readonly INoteBookService appService;

        public NoteBookModel Model
        {
            get { return model; }
            set { model = value; RaisePropertyChanged(); }
        }

        public override async void Save()
        {
            if (Model.Id == 0)
                await appService.Create(Model);
            else
                await appService.Update(Model);

            base.Save();
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters.ContainsKey("Value")) //编辑操作
                Model = parameters.GetValue<NoteBookModel>("Value");
            else //新增操作
                Model = new NoteBookModel();
        }
    }
}
